/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.login.niceshot;

import br.com.niceshot.Conexao;
import br.com.niceshot.PassToMD5;
import br.com.niceshot.PresetQueries;
import static java.lang.System.out;
import java.util.LinkedHashMap;

/**
 *
 * @author Alexandre
 */
public class LoginQueries extends PresetQueries {

    public String SelectLogin(String usuario, String senha) {
        try {
            con = Conexao.getConnection();
            stat = con.createStatement();
            PassToMD5 senhaMD5 = new PassToMD5();

            rs = stat.executeQuery("SELECT id_usuario, ic_situacao FROM Usuario WHERE nm_usuario='" + usuario + "' and cd_senha='" + senhaMD5.CriptografaSenha(senha) + "'");

            if (rs.next()) {
                if (rs.getString(2).equals("I")) {
                    return rs.getString(2);
                } else {
                    return NivelAcesso(rs.getString(1));
                }

            }

        } catch (Exception e) {
            out.println(e.getMessage());

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }

        return "";
    }

    public String NivelAcesso(String idUsuario) {
        try {
            con = Conexao.getConnection();
            stat = con.createStatement();
            rs = stat.executeQuery("SELECT nm_tipo_usuario FROM tipo_usuario AS t "
                    + " INNER JOIN usuario AS u ON t.id_tipo_usuario = u.id_tipo_usuario "
                    + " WHERE u.id_usuario = " + idUsuario);

            if (rs.next()) {
                if (rs.getString(1).indexOf("Adm") > -1) {
                    return "A";
                } else {
                    return "C";
                }
            }

        } catch (Exception e) {
            out.println(e.getMessage());

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }

        return "";
    }

    public String updateSenha(String user, String senhaAtual, String senhaNova) {
        try {
            con = Conexao.getConnection();
            stat = con.createStatement();
            rs = stat.executeQuery("SELECT id_usuario, cd_senha FROM Usuario WHERE nm_usuario = '" + user + "'");
            PassToMD5 senhaMD5 = new PassToMD5();

            if (rs.next()) {
                if (rs.getString(2).equals(senhaMD5.CriptografaSenha(senhaAtual))) {
                    LinkedHashMap<String, String> dataUsuario = new LinkedHashMap<>();
                    senhaNova = senhaMD5.CriptografaSenha(senhaNova);
                    dataUsuario.put("id_usuario", rs.getString(1));
                    dataUsuario.put("cd_senha", "'" + senhaNova + "'");
                    return UpdateData("usuario", dataUsuario);
                }
            }

        } catch (Exception e) {
            out.println(e.getMessage());

        } finally {
            try {
                rs.close();
            } catch (Exception e) {
            }
            try {
                con.close();
            } catch (Exception e) {
            }
            try {
                stat.close();
            } catch (Exception e) {
            }
        }

        return "Error";
    }

}
